/**
 * 荣誉成就信息JS
 * @author Assassin
 */
$(document).ready(function(){
    $('#uploadPictureLink').on('click',function(){
        $('#pictureFile').click();
    });
    $('#pictureImg').on('click',function(){
        $('#pictureFile').click();
    });
});

// 上传缩略图片
function uploadPicture(){
    $.ajaxFileUpload({
        url:shiro.baseUrl + "uploadPicture",
        secureuri:false,
        fileElementId:"pictureFile",
        dataType: 'json',
        success: function (data) {
            $("#pictureImg").attr("src", data.preUrl + "?v=" + new Date());
            $('#picture').val(data.picture);
        },error:function(XMLHttpRequest, textStatus, errorThrown){
            layer.msg(errorThrown, {icon: 5,time: 1000});
        }
    });
}

// 执行添加荣誉成就信息
function excuteAddHonourInfo() {
    var title = $.trim($('#title').val());
    if(shiro.util.isEmpty(title)) {
        layer.tips('荣誉成就标题不能为空', '#title');
        return false;
    }
    var desc = $.trim($('#desc').val());
    if(shiro.util.isEmpty(desc)) {
        layer.tips('荣誉成就简介不能为空', '#desc');
        return false;
    }
    var content = CKEDITOR.instances.content_editor.getData();
    if(shiro.util.isEmpty(content)) {
        layer.msg('荣誉成就内容不能为空', {icon: 5,time: 1000});
        return false;
    }
    var picture = $('#picture').val();
    if(shiro.util.isEmpty(picture)) {
        layer.msg('缩略图片不能为空', {icon: 5,time: 1000});
        return false;
    }
    // 提交信息
    shiro.ajax({
        url: 'honour/executeAddHonour',
        data:{'title': title, "content":content,'desc':desc,"picture":picture},
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "honour/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#title').val('');
            }
        }
    });
    return false;
}

// 执行修改荣誉成就信息
function excuteEditHonourInfo() {
    var title = $.trim($('#title').val());
    if(shiro.util.isEmpty(title)) {
        layer.tips('荣誉成就标题不能为空', '#title');
        return false;
    }
    var desc = $.trim($('#desc').val());
    if(shiro.util.isEmpty(desc)) {
        layer.tips('荣誉成就简介不能为空', '#desc');
        return false;
    }
    var content = CKEDITOR.instances.content_editor.getData();
    if(shiro.util.isEmpty(content)) {
        layer.msg('荣誉成就内容不能为空', {icon: 5,time: 1000});
        return false;
    }
    var picture = $('#picture').val();
    if(shiro.util.isEmpty(picture)) {
        layer.msg('缩略图片不能为空', {icon: 5,time: 1000});
        return false;
    }
    var honourId = $('#honourId').val();
    // 提交信息
    shiro.ajax({
        url: 'honour/executeEditHonour',
        data:{'honourId': honourId, 'title': title, "content":content,'desc':desc,"picture":picture},
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "honour/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#title').val('');
            }
        }
    });
    return false;
}

// 删除荣誉成就
function deleteHonour(honourId) {
    layer.msg('确定删除该荣誉成就？', {
        time: 0,
        btn: ['确定', '取消'],
        yes: function(index){
            layer.close(index);
            // 提交删除
            shiro.ajax({
                url: 'honour/delete',
                data:{"honourId": honourId},
                success: function(data) {
                    if(data.result == 0) {
                        layer.msg(data.message, {icon: 1,time: 1000}, function() {
                            window.location.href = shiro.baseUrl + "honour/list";
                        });
                    } else {
                        layer.msg(data.message, {icon: 5,time: 1000});
                    }
                }
            });
        }
    });
}

// 批量删除荣誉成就
function batchDeleteHonours() {
    var honourIdArray = shiro.form.getCheckBoxSelectValues('honourIdCheckBox');
    if(honourIdArray.length == 0) {
        layer.msg('请选择要操作的数据行', {icon: 5,time: 1000});
    } else {
        layer.msg('确定删除选中的荣誉成就？', {
            time: 0,
            btn: ['确定', '取消'],
            yes: function(index){
                layer.close(index);
                var honourIds = honourIdArray.join(',');
                // 提交删除
                shiro.ajax({
                    url: 'honour/batchDelete',
                    data:{"honourIds": honourIds},
                    success: function(data) {
                        if(data.result == 0) {
                            layer.msg(data.message, {icon: 1,time: 1000}, function() {
                                window.location.href = shiro.baseUrl + "honour/list";
                            });
                        } else {
                            layer.msg(data.message, {icon: 5,time: 1000});
                        }
                    }
                });
            }
        });
    }
}